package com.rykj.qxj.rest;

import com.github.pagehelper.PageInfo;
import com.rykj.qxj.annotation.EnableSystemLog;
import com.rykj.qxj.server.model.entity.Notice;
import com.rykj.qxj.server.services.INoticeManageService;
import com.rykj.qxj.server.util.object.StringUtils;
import com.rykj.qxj.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Log4j
@RestController
@RequestMapping("/v1/noticeManage")
@Api(tags="系统公告管理")
public class NoticeManageController {
    @Autowired
    private INoticeManageService noticeManageService;

    @PostMapping("/saveNotice")
    @ApiOperation(value="创建公告信息",notes ="创建公告信息")
    @EnableSystemLog(value="创建公告信息",fullPath ="/v1/noticeManage/saveNotice")
    public Result saveNotice(@RequestBody Notice notice){
        try{
            noticeManageService.saveNotice(notice);
            return new Result(true,"添加成功");
        }catch (Exception e){
            log.error("添加失败", e);
            return new Result(false, "添加失败");
        }
    }
    @PostMapping("/updateNotice")
    @ApiOperation(value="修改公告信息",notes ="修改公告信息")
    @EnableSystemLog(value="修改公告信息",fullPath ="/v1/noticeManage/updateNotice")
    public Result updateNotice(@RequestBody Notice notice){
        try{
            noticeManageService.updateNotice(notice);
            return new Result(true,"修改成功");
        }catch (Exception e){
            log.error("修改失败", e);
            return new Result(false, "修改失败");
        }
    }
    @GetMapping("/updateIsEnable")
    @ApiOperation(value="停用或启用公告",notes ="停用或启用公告")
    @EnableSystemLog(value="停用或启用公告",fullPath ="/v1/noticeManage/updateNotice")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noticeId", value = "元数据id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "isEnable", value = "是否启用:Y启用，N停用",required = true,paramType = "query")
    })
    public Result updateIsEnable(@RequestParam(value = "noticeId") Integer noticeId,
                                 @RequestParam(value = "isEnable") String isEnable){
        try{
            noticeManageService.updateIsEnable(noticeId,isEnable);
            return new Result(true,"停用或启用公告成功");
        }catch (Exception e){
            log.error("停用或启用公告失败", e);
            return new Result(false, "停用或启用公告失败");
        }
    }


    @GetMapping("/deleteNoticeById")
    @ApiOperation(value="批量删除",notes ="批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noticeIds", value = "公告id,逗号隔开",required = true,paramType = "query")
    })
    @EnableSystemLog(value="批量删除",fullPath ="/v1/noticeManage/deleteNoticeById")
    public Result deleteNoticeById( @RequestParam(value = "noticeIds") String noticeIds){
        try{
            if (StringUtils.isEmpty(noticeIds))
            {
                return new Result(false,"请选择要删除的公告");
            }
            noticeManageService.deleteNoticeById(noticeIds);
            return new Result(true,"删除成功");
        }catch (Exception e){
            log.error("删除失败", e);
            return new Result(false, "删除失败");
        }
    }


    @GetMapping("/getNoticeList")
    @ApiOperation(value="分页获取公告列表",notes ="分页获取公告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "显示行数",required = true,paramType = "query"),
            @ApiImplicitParam(name = "title", value = "标题",paramType = "query"),
            @ApiImplicitParam(name = "isEnable", value = "元数据id",paramType = "query")
    })
    public Result getNoticeList(   @RequestParam(value = "pageNum") Integer pageNum,
                                   @RequestParam(value = "pageSize") Integer pageSize,
                                   @RequestParam(value = "title",required = false) String title,
                                   @RequestParam(value = "isEnable",required = false) String isEnable){
        try{
          Integer count =  noticeManageService.getNoticeListCount(title,isEnable);
          Integer pageCount = (count + pageSize - 1)/pageSize +1;
          if(pageNum>pageCount){
              log.error("页数不能超过总页数,总页数为:"+pageCount );
              return new Result(false,"页数不能超过总页数,总页数为:"+pageCount);
          }
          PageInfo pageInfo =  noticeManageService.getNoticeList(pageNum,pageSize,title,isEnable);
          return new Result(pageInfo);
        }catch (Exception e){
            log.error("分页获取公告列表失败", e);
            return new Result(false, "分页获取公告列表失败");
        }
    }

    @GetMapping("/getNoticeDetails")
    @ApiOperation(value="获取公告详情",notes ="获取公告详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noticeId", value = "元数据id",required = true,paramType = "query")
    })
    public Result getNoticeDetails(@RequestParam(value = "noticeId") Integer noticeId){
        try{
          Notice notice=  noticeManageService.getNoticeDetails(noticeId);
          return new Result(notice);
        }catch (Exception e){
            log.error("获取公告详情失败", e);
            return new Result(false, "获取公告详情失败");
        }
    }
    @GetMapping("/updateReadNum")
    @ApiOperation(value="修改阅读数量",notes ="修改阅读数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "readNum", value = "阅读数量",required = true,paramType = "query"),
            @ApiImplicitParam(name = "noticeId", value = "元数据id",required = true,paramType = "query")
    })
    @EnableSystemLog(value="修改阅读数量",fullPath ="/v1/noticeManage/updateReadNum")
    public Result updateReadNum(@RequestParam(value = "readNum") Integer readNum,
                                @RequestParam(value = "noticeId") Integer noticeId){
        try{
             noticeManageService.updateReadNum(readNum,noticeId);
            return new Result( true,"修改成功");
        }catch (Exception e){
            log.error("修改失败", e);
            return new Result(false, "修改失败");
        }
    }


}
